ImageGear Java PDF
Get PDF Page Size

To get the page size of a PDF page in a document:

  1. Use the createDocument method of the PDF class to create a new instance of the Document class.
  2. Use the openDocument method of the Document class to load a PDF file.
  3. Use the getPage method of the Document class to obtain the PDF page for which you want to get the size.
  4. Use getWidth and getHeight methods of the Page class to get the page's crop box width and height.

The following is an illustration of how to get the page size of the previously loaded PDF document:

 
Copy Code
import com.accusoft.imagegearpdf.*;

class PdfDemo
{
        private PDF pdf;
        private Document document;

        // Obtain size of the PDF page.
      	public PageSize GetPageSize(long pageNumber)
        {
                Page page = null;

                try
                {
                        // Retrieve specific page.
                        page = document.getPage(pageNumber);

			PageSize size = new PageSize();
			size.Width = page.getWidth();
			size.Height = page.getHeight();

                        return size;
                }
 	        catch (Throwable ex)
                {
      	        	// Failed to get page size.
                        System.err.println("Exception: " + ex.toString());
                        return null;
  	        }
                finally
                {
                        if (page != null)
                        {
      	        	        // Close PDF page as it is not needed anymore.
                                page.close();
                        }
                }
        }

        // Class to store page size.
        public class PageSize
        {
                // Page width.
                public double Width;

                // Page height.
                public double Height;
        }
}

See Also

Open a PDF Document for details about how to open and close a PDF document

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback